package com.bourne.controller;


import com.bourne.model.BaseCoreDO;
import com.bourne.service.BaseCoreService;
import com.bourne.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @program core-service
 * @description 基础类
 * @author: bourne
 * @create 2021-11-01 12:36:41
 **/
@RestController
@Slf4j
public class BaseCoreController<S extends BaseCoreService, T extends BaseCoreDO> {

    @Autowired(required = false)
    protected S service;

    @Autowired
    private StringEncryptor encryptor;

    /**
     * 获取jasypt的加密信息
     * @param dataMap
     * @return "password": "of16aZGOugMVZhes0kTZXM4B0lxTAQmj",
     *         "root": "LtRwlJUjLSA0bxoUeq0X6Q==",
     *         "url": "AI2saHab0s4V2CPwR5Wt/95V7xlJijjCwnjTEeTebgqA4zaNOmv/q0yvW8TtJ2aRrL+5tv1/p7UipshlzHcR4qEUyM+BTCTQY2OdiG6h7FRBCsXw+P68BA=="
     */
    @GetMapping("/base/jasypt")
    public JsonData getJasyptData(@RequestBody Map<String, String> dataMap) {
        if (dataMap == null || dataMap.isEmpty()) {
            return null;
        }
        Map<String, String> reMap = new HashMap();
        dataMap.forEach((key, value) -> {
            reMap.put(key, encryptor.encrypt(value));
        });
        return JsonData.buildSuccess(reMap);
    }

}
